/*
Package v1
@Time : 2022/12/21 16:28
@Author : 董胜烨
@File : attendance
@Software: GoLand
@note:
*/
package v1

import (
	"employee/service"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"strconv"
)

func InsertAttendance(c *gin.Context) {
	attendance := &service.Attendance{}
	err := c.BindJSON(attendance)
	if err != nil {
		return
	}
	ok := attendance.Insert()
	if ok {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "增加成功",
			"code": 0,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "增加失败",
			"code": 1,
		})
	}

}

func UpdateAttendance(c *gin.Context) {
	attendance := &service.Attendance{}
	err := c.BindJSON(attendance)
	if err != nil {
		return
	}
	ok := attendance.Update()
	if ok {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "修改成功",
			"code": 0,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "修改失败",
			"code": 1,
		})
	}
}

func DeleteAttendance(c *gin.Context) {
	attendance := &service.Attendance{}
	err := c.BindJSON(attendance)
	if err != nil {
		log.Println("DeleteAttendance error", err)
		return
	}
	ok := attendance.Delete()
	if ok {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "删除成功",
			"code": 0,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "删除失败",
			"code": 1,
		})
	}
}

func QueryAttendance(c *gin.Context) {
	attendance := &service.Attendance{}
	attendance.EmployeeID, _ = strconv.ParseInt(c.DefaultQuery("employee_id", "0"), 10, 64)
	res := attendance.QueryByEmployeeId()
	if res != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "获取成功",
			"data": res,
			"code": 0,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "获取失败",
			"code": 1,
		})
	}

}
